import axios from "axios";
// import qs from "qs";

// axios.defaults.baseURL = 'http://xxktd.sprout-edu.com'  //正式
if (process.env.NODE_ENV == 'development') {    
  axios.defaults.baseURL = '/pro';
}  else if (process.env.NODE_ENV == 'production') {    
  axios.defaults.baseURL = 'http://xxktd.sprout-edu.com';
}
//post请求头
axios.defaults.headers.post["Content-Type"] =
  "application/json;charset=UTF-8";
axios.defaults.headers.post["X-Auth-Token"] =
  "LSk8Atw3JUxUgmMA8EhY6UT2VPSdmhe889sjvneHlyc=";
//设置超时
axios.defaults.timeout = 10000;

axios.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  response => {
    if (response.status == 200) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  error => {
    console.log(error)
  }
);
export default {
  post(url, data) {
    if(data.type){
      url = 'https://xxktd.sprout-edu.com' + url
      delete data.type
    }
    if(data.isDownload){
      url = 'http://api.sprout-edu.com:8082' + url
      delete data.isDownload
    }
    return new Promise((resolve, reject) => {
      axios({
          method: 'post',
          url,
          data: data,
        })
        .then(res => {
          return resolve(res.data)
        })
        .catch(err => {
          return reject(err)
        });
    })
  },

  get(url, data) {
    return new Promise((resolve, reject) => {
      axios({
          method: 'get',
          url,
          params: data,
        })
        .then(res => {
          resolve(res.data)
        })
        .catch(err => {
          reject(err)
        })
    })
  }
};
